//
//  SN_SearchResultCell.m
//  Shiyun_Newspaper
//
//  Created by Sylar on 2017/6/16.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_SearchResultCell.h"

@implementation SN_SearchResultCell {
    
    UIImageView *subImg;
    UILabel *paperNameLabel;
    UILabel *subjectLabel;
    UILabel *gradeLabel;
    UIImageView *rightImg;
    UILabel *rightLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        subImg = [[UIImageView alloc]initWithFrame:CGRectMake(15, 16, 50, 66)];
        [self.contentView addSubview:subImg];
        
        paperNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(subImg.right + 12, 19, SCREENWIDTH - 80 -62, 16)];
        [paperNameLabel setFont:[UIFont systemFontOfSize:16]];
        [self.contentView addSubview:paperNameLabel];
        
        subjectLabel = [[UILabel alloc]initWithFrame:CGRectMake(paperNameLabel.x, paperNameLabel.bottom+8, paperNameLabel.width, 14)];
        [subjectLabel setFont:[UIFont systemFontOfSize:14]];
        [subjectLabel setTextColor:colorFromHex(@"979797")];
        [self.contentView addSubview:subjectLabel];
        
        gradeLabel = [[UILabel alloc]initWithFrame:CGRectMake(paperNameLabel.x, subjectLabel.bottom+8, paperNameLabel.width, 14)];
        [gradeLabel setFont:[UIFont systemFontOfSize:14]];
        [gradeLabel setTextColor:colorFromHex(@"979797")];
        [self.contentView addSubview:gradeLabel];
        
        rightImg = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH - 70, 26, 40, 28)];
        [rightImg setImage:[UIImage imageNamed:@"search_Start"]];
        [self.contentView addSubview:rightImg];
        
        rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH - 124, rightImg.bottom+6, 100, 13)];
        [rightLabel setFont:[UIFont systemFontOfSize:13]];
        rightLabel.textAlignment = NSTextAlignmentRight;
        [rightLabel setTextColor:colorFromHex(@"fb524a")];
        [self.contentView addSubview:rightLabel];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(15, 97.5, SCREENWIDTH, 0.5)];
        [line setBackgroundColor:colorFromHex(@"#dedede")];
        [self.contentView addSubview:line];
        
    }
    
    return self;
}

- (void)creatCell:(SN_NewspaperModel *)model type:(NSInteger )type searchContent:(NSString *)searchContent {
    
    [subImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%ld",IMAGEHOSTURL,model.pic]] placeholderImage:[UIImage imageNamed:@"newspaper_default"]];
    
    NSString *userNameStr = model.newsname;
    NSMutableArray *arry=[[NSMutableArray alloc]initWithArray:[Utils getRangeStr:userNameStr findText:searchContent]];
    
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:userNameStr];
    for (int i=0; i<arry.count; i++) {
        
        NSMutableAttributedString *attributedStr01 = [[NSMutableAttributedString alloc] initWithAttributedString:attributedStr];
        NSRange  range=NSMakeRange([[arry objectAtIndex:i] intValue], searchContent.length);
        
        [attributedStr01 addAttribute: NSForegroundColorAttributeName value:colorFromHex(@"fb524a") range: range];
        attributedStr=attributedStr01;
    }
    paperNameLabel.attributedText=attributedStr;
    
    subjectLabel.text = [NSString stringWithFormat:@"%@ 第%ld期",model.coursename,model.phase];
    
    gradeLabel.text = model.gradername;
    
    rightLabel.text = type == 0?@"开始阅读":@"开始解题";
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
